package com.imarkket.Library;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Sistemas on 23/05/2014.
 */
public class c_connection_checking {

    public ProgressDialog dialogProcess = null;

    private static c_connection_checking instance = new c_connection_checking();
    static Context context;

    public static c_connection_checking getInstance(Context ctx) {
        context = ctx;
        return instance;
    }

    public boolean CheckWIFI() {
        ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        return wifi.isConnected();
    }

    public boolean CheckMOBILE() {
        ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        return mobile.isConnected();
    }

    public boolean CheckInternetURL(String str_URL) {

        Boolean ret = false;

        try {
            URL url = new URL(str_URL.toString());
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000); // This is time limit if the
            // connection time limit
            urlc.connect();
            if (urlc.getResponseCode() == 200) {
                ret = true;
            } else {
                ret = false;
            }
        } catch (MalformedURLException e) {
            c_logging.getInstance().log(c_logging.LOG_ERROR, "CheckInternetURL MalformedURLException: " + e.toString());
            ret = false;
        } catch (IOException e) {
            c_logging.getInstance().log(c_logging.LOG_ERROR, "CheckInternetURL IOException: " + e.toString());
            ret = false;
        }
        return ret;
    }
}